package com.common.www.bean.entity.district;

import org.springframework.boot.base.jpa.domain.entity.NumberIdentityEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Where;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 地区
 */
@DynamicUpdate(false)
@DynamicInsert(false)
@Getter
@Setter
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(indexes = {@Index(columnList = "level")})
@JsonIgnoreProperties(value = {})
public class ProvinceCity extends NumberIdentityEntity {

    private static final long serialVersionUID = -2278084748058910042L;

    @JsonIgnoreProperties(value = {"parent", "children"})
    @ManyToOne()
    @JoinColumn(name = "parent")
    private ProvinceCity parent;

    //二级分类
    @JsonIgnoreProperties(value = {"parent", "children"})
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "parent")
    @OrderBy("sort asc")
    @Where(clause = "flag = 1")
    private List<ProvinceCity> children;

    /**
     * 类型名称名称
     */
    @Column(length = 128)
    private String name;
    /**
     * 节点地址
     */
    @Column(length = 256)
    private String remark;
    /**
     * 行政编码
     */
    @Column(length = 30)
    private Long code;

    /**
     * 排序数字大小，数字越小排列在越前面
     */
    @Column(length = 30)
    private Integer sort;
    /**
     * 节点等级
     */
    @Column(length = 30)
    private Integer level;

    /**
     * 可以逻辑删除
     */
    @Column(length = 1, columnDefinition = "INTEGER DEFAULT 1")
    private Integer flag;

    public Integer getSort() {
        if (sort == null)
            sort = 0;
        return sort;
    }

}
